package com.anuprayog.convertunits;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class EnergyActivity extends Activity {

	private EditText text, result;
	private Spinner spin1, spin2;
	private TextView resultString, resultString1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_energy);
		
		text = (EditText) findViewById(R.id.editText1);
		spin1 = (Spinner) findViewById(R.id.spinner1);
		spin2 = (Spinner) findViewById(R.id.spinner2);
		result = (EditText) findViewById(R.id.editText2);
		resultString = (TextView) findViewById(R.id.textView4);
		resultString1 = (TextView) findViewById(R.id.textView5);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.energy, menu);
		return true;
	}

	/*@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}*/

	//final Button button = (Button) findViewById(R.id.button1);
    // button.setOnClickListener(new View.OnClickListener() {
    	
	public void onClick(View view) {
			if (text.getText().length() == 0) {
				Toast.makeText(this, "Please enter a valid number",
						Toast.LENGTH_LONG).show();
				return;
			}

			double input = Double.parseDouble(text.getText().toString());
			String unit1 = spin1.getSelectedItem().toString();
			String unit2 = spin2.getSelectedItem().toString();
			double joulevalue;
			double d;
			
			if (unit1.equals("Joule"))
			{
				joulevalue = input;
			}
			
			else if (unit1.equals("Kilojoule"))
			{
				joulevalue = input*(1000);
			}
			
			else if (unit1.equals("Calorie"))
			{
				joulevalue = input*(4.1868);
			}
			
			else if (unit1.equals("Kilocalorie"))
			{
				joulevalue = input*(4186.8);
			}
			
			else if (unit1.equals("Erg"))
			{
				joulevalue = input*(0.0000001);
			}
			
			else if (unit1.equals("Watt Hour"))
			{
				joulevalue = input*(3600);
			}
			
			else if (unit1.equals("Kilowatt Hour"))
			{
				joulevalue = input*(3600000);
			}
			
			else if (unit1.equals("Meter Kilogram Force"))
			{
				joulevalue = input*(9.80665);
			}
			
			else if (unit1.equals("Foot Pound Force"))
			{
				joulevalue = input*(1.3558179483314004);
			}
			
			else if (unit1.equals("Foot Poundal"))
			{
				joulevalue = input*(0.0421401100938048);
			}
			
			else if (unit1.equals("BTU"))
			{
				joulevalue = input*(1055.05585262);
			}
			
			else if (unit1.equals("Therm"))
			{
				joulevalue = input*(105505585.262);
			}
			
			else if (unit1.equals("Litre Atmosphere"))
			{
				joulevalue = input*(101.325);
			}
			
			else if (unit1.equals("Horsepower Hour"))
			{
				joulevalue = input*(2684519.537696172792);
			}
			
			else if (unit1.equals("Electron Volt"))
			{
				joulevalue = input*(1.60217733E-19);
			}
			
			else if (unit1.equals("Atomic unit of energy"))
			{
				joulevalue = input*(4.359744E-18);
			}
			
			else
			{
				joulevalue = 0;
			}
			
			
			if (unit2.equals("Joule")){
				d = joulevalue;
			}
			
			else if (unit2.equals("Kilojoule")){
				d = joulevalue*(0.001);
			}
			
			else if (unit2.equals("Calorie")){
				d = joulevalue*(1/4.1868);
			}
			
			else if (unit2.equals("Kilocalorie")){
				d = joulevalue*(1/4186.8);
			}
			
			else if (unit2.equals("Erg")){
				d = joulevalue*(10000000);
			}
			
			else if (unit2.equals("Watt Hour")){
				d = joulevalue*(1/3600d);
			}
			
			else if (unit2.equals("Kilowatt Hour")){
				d = joulevalue*(1/3600000d);
			}
			
			else if (unit2.equals("Meter Kilogram Force")){
				d = joulevalue*(1/9.80665);
			}
			
			else if (unit2.equals("Foot Pound Force")){
				d = joulevalue*(1/1.3558179483314004);
			}
			
			else if (unit2.equals("Foot Poundal")){
				d = joulevalue*(1/0.0421401100938048);
			}
			
			else if (unit2.equals("BTU")){
				d = joulevalue*(1/1055.05585262);
			}
			
			else if (unit2.equals("Therm")){
				d = joulevalue*(1/105505585.262);
			}
			
			else if (unit2.equals("Litre Atmosphere")){
				d = joulevalue*(1/101.325);
			}
			
			else if (unit2.equals("Horsepower Hour")){
				d = joulevalue*(1/2684519.537696172792);
			}
			
			else if (unit2.equals("Electron Volt")){
				d = joulevalue*(6241506363094027800.281008844383);
			}
			
			else if (unit2.equals("Atomic unit of energy")){
				d = joulevalue*(229371265835792193.30309302564554);
			}
			
			else
			{
				d = 0;
			}
		
			result.setText(String.valueOf(d));
			
			resultString.setText(String.valueOf(input) + " " + unit1 + " =");
			resultString1.setText(String.valueOf(d) + " " + unit2);
			
		}
	
	
	public void goBack(View view) {
		finish();
	}
		
	public void goSwap(View view) {
		int spin1Index = spin1.getSelectedItemPosition();

		spin1.setSelection(spin2.getSelectedItemPosition());
		spin2.setSelection(spin1Index);
	}
}
